//
//  XYTUploadLicenseCell.m
//  XYTPayOrderSystem
//
//  Created by wupeng on 2017/8/30.
//  Copyright © 2017年 wupeng. All rights reserved.
//

#import "XYTUploadLicenseCell.h"
#import <AssetsLibrary/AssetsLibrary.h>

@interface XYTUploadLicenseCell ()

@end

@implementation XYTUploadLicenseCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updataImage:) name:@"licenseImgChooseFinished" object:nil];
    }
    return self;
}

- (void)setupUI{
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(20, 2.5, 90, 20)];
    titleLab.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:titleLab];
    self.titleLab = titleLab;
    
    UIButton *chooseImgBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(titleLab.frame)+5, 2.5, 65, 20)];
    chooseImgBtn.backgroundColor = [UIColor colorWithRed:226.0f/255.0 green:225.0f/255.0 blue:226.0f/255.0 alpha:1];
    [chooseImgBtn addTarget:self action:@selector(onClickChooseFile) forControlEvents:UIControlEventTouchUpInside];
    [chooseImgBtn setTitle:@"选择文件" forState:UIControlStateNormal];
    [chooseImgBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    chooseImgBtn.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [self.contentView addSubview:chooseImgBtn];
    self.chooseBtn = chooseImgBtn;
    
    UILabel *fileNameLab = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(chooseImgBtn.frame)+3, 2.5, kScreen_Width-CGRectGetMaxX(chooseImgBtn.frame)-3, 20)];
    fileNameLab.font = [UIFont systemFontOfSize:12.0];
    fileNameLab.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview:fileNameLab];
    self.fileNameLab = fileNameLab;
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(titleLab.frame)+30, CGRectGetMaxY(chooseImgBtn.frame)+1, 100, 130)];
    imgView.image = [UIImage imageNamed:@"image_default"];
    [self.contentView addSubview:imgView];
    self.imgView = imgView;
    
}

- (void)setStarTitle:(NSString *)title{
    NSRange range = [title rangeOfString:@"*"];
    if (range.location != NSNotFound) {
        NSString *starTitle = [title stringByAppendingString:@"*"];
        NSMutableAttributedString * attStr = [[NSMutableAttributedString alloc] initWithString:starTitle];
        NSRange range = [@"*" rangeOfString:starTitle];
        [attStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
        [self.titleLab setAttributedText:attStr];
        return;
    }
    self.titleLab.text = title;
}

- (void)onClickChooseFile{
    if (self.delegate && [self.delegate respondsToSelector:@selector(onClickChooseFile)]) {
        [self.delegate onClickChooseFile];
    }
}

- (void)updataImage:(NSNotification *)notification{
    NSDictionary *imageDic = notification.userInfo;
    NSURL *imageUrl = imageDic[@"imageUrl"];
    NSData *imageData = imageDic[@"imageData"];
    __block NSString *fileName;
    XYTWeakSelf(self)
    //使用assetsLibrary 获得相册所选图片名称
    ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
    [assetsLibrary assetForURL:imageUrl resultBlock:^(ALAsset *asset) {
        XYTStrongSelf(weakSelf)
        ALAssetRepresentation *representation = [asset defaultRepresentation];
        fileName = representation.filename;
        strongSelf.fileNameLab.text = fileName;
    } failureBlock:^(NSError *error) {
        NSLog(@"%@", [error localizedDescription]);
    }];
    dispatch_async(dispatch_get_main_queue(), ^{
        if (imageData) {
            self.imgView.image = [UIImage imageWithData:imageData];
        }
    });
}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}




- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
